/**
 * Created by isoft-work 5 on 1/17/2015.
 */
$(document).ready(function(){
   bindEventReportRevenue();
});

function bindEventReportRevenue() {
   ISS.EVENT.bindEvent('#result_report', 'click', function () {
      getRevenueReport();
   });

   //ISS.EVENT.bindEvent('#option_report', 'change', function () {
   //   var _option = ISS.DOM.getVal("#option_report");
   //   if(_option == CONSTANT.ReportProduct.Sale) {
   //      ISS.DOM.show('#result_report_revenue_chart');
   //      ISS.DOM.hide('#result_report_revenue');
   //   }
   //   else {
   //      ISS.DOM.show('#result_report_revenue');
   //      ISS.DOM.hide('#result_report_revenue_chart');
   //   }
   //});
}

function getRevenueReport() {
   var _option = ISS.DOM.getVal("#option_report");
   var _date = ISS.DOM.getVal("#input_name_time");
   if(_option && _date) {
      if(_option == CONSTANT.ReportProduct.Remain) {
         getDayRevenueReport(_date);
      }
      else if(_option == CONSTANT.ReportProduct.RevenueMonth) {
         getMonthRevenueReport(_date);
      }
      else if(_option == CONSTANT.ReportProduct.Sale) {
         getQuarterlyRevenueReport(_date);
      }
   }
   else {
      alert('Chưa chọn thông kê hoặc thời gian');
   }
}

function getDayRevenueReport(date) {
   ISS.BACKEND.request({
      url: '../../service/admin/report/getRevenueForDate/' + date,
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseContent = data.responseObject;
         mapDayRevenueToView(responseContent);
      }
   });
}

function getMonthRevenueReport(date) {
   ISS.BACKEND.request({
      url: '../../service/admin/report/getRevenueForMonth/' + date,
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseContent = data.responseObject;
         mapMonthRevenueToView(responseContent);
      }
   });
}

function getQuarterlyRevenueReport(date) {
   ISS.BACKEND.request({
      url: '../../service/admin/report/getRevenueForQuarterly/' + date,
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      dataType: 'json',
      success: function (data) {
         var responseContent = data.responseObject;
         mapQuarterlyRevenueToView(responseContent);
      }
   });
}

function mapDayRevenueToView(dayRevenue) {
   if(dayRevenue.length > 0) {
      var _html = '';
      for(var i = 0; i < dayRevenue.length; i++) {
         _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.RevenueReport.dayList, dayRevenue[i]);
      }
      ISS.DOM.html('#header_report_revenue', TEMPLATE.RevenueReport.dayHeader);
      ISS.DOM.html('#list_report_revenue', _html);
      ISS.DOM.pagingAdmin('#result_report_revenue');
      ISS.DOM.show('#paging');
   }
}

function mapMonthRevenueToView(monthRevenue) {
   if(monthRevenue.length > 0) {
      var _html = '';
      for(var i = 0; i < monthRevenue.length; i++) {
         _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.RevenueReport.monthList, monthRevenue[i]);
      }
      ISS.DOM.html('#header_report_revenue', TEMPLATE.RevenueReport.monthHeader);
      ISS.DOM.html('#list_report_revenue', _html);
      ISS.DOM.pagingAdmin('#result_report_revenue');
      ISS.DOM.show('#paging');
   }
}

function mapQuarterlyRevenueToView(quarterlyRevenue) {
   if(quarterlyRevenue.length > 0) {
      var _html = '';
      var count = 0;
      var total = 0;
      var quarter = '', index = 0;
      for(var i = 0; i < quarterlyRevenue.length; i++) {
         count += 1;
         total += quarterlyRevenue[i].tien;
         _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.RevenueReport.quarterList, quarterlyRevenue[i]);
         if(count == 3) {
            index += 1;
            var _quarterlyRevenue = {};
            _quarterlyRevenue.quarter = 'Quý ' + index ;
            _quarterlyRevenue.total = total;
            _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.RevenueReport.quarterList, _quarterlyRevenue);
            total = 0;
            count = 0;
         }
      }
      //ISS.DOM.html('#header_report_revenue', TEMPLATE.RevenueReport.monthHeader);
      ISS.DOM.html('#list_report_revenue', _html);
      ISS.DOM.pagingAdmin('#result_report_revenue');
      ISS.DOM.show('#paging');
   }
}

function drawChart(ruleOnput) {
   var _html = '\nBiểu đồ\tTập luật';
   for(var i = 0; i < ruleOnput.length; i++) {
      _html += '\n' + ruleOnput[i].luat;
      _html += '\t' + ruleOnput[i].phanTram + '%';
   }
   ISS.DOM.html('#tsv', _html);
   ISS.GRAPH.chart('tsv');
}

